//菜单
function createContextMenu(map) {
	contextmenu = document.getElementById('popupmenu');
    //触发右击事件
    GEvent.addListener(map, "singlerightclick", function(pixel, tile , marker) {
    	if(marker){
    		map.removeOverlay(marker);
    	}
    	else{
    		clickedPixel = pixel;
    	     
            var x = pixel.x;
            var y = pixel.y;
            if (x > map.getSize().width - 120) {
                x = map.getSize().width - 120;
            }
            if (y > map.getSize().height - 100) {
                y = map.getSize().height - 100;
            }
            var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x, y));
            pos.apply(contextmenu);
            
            contextmenu.style.display='block'; //Showing the menu
    	}
    });
    //隐藏右键菜单
    GEvent.addListener(map, "click", function() {
    	hidePopupMenu();
    });
}
//隐藏menu
function hidePopupMenu(){
	document.getElementById('popupmenu').style.display='none';
}

//放大    
function zoomIn() {
    map.zoomIn();
    contextmenu.style.display = "none";
}
//缩小
function zoomOut() {
    map.zoomOut();
    contextmenu.style.display = "none";
}
//居中
function centreMapHere() {
    var point = map.fromContainerPixelToLatLng(clickedPixel);
    map.setCenter(point);
    contextmenu.style.display = "none";
}

//以此为中心放大实现
function zoomInHere() {
    var point = map.fromContainerPixelToLatLng(clickedPixel);
    map.zoomIn(point, true);
    contextmenu.style.display = "none";
}
//以此为中心缩小实现
function zoomOutHere() {
    var point = map.fromContainerPixelToLatLng(clickedPixel);
    map.setCenter(point, map.getZoom() - 1);
    contextmenu.style.display = "none";
}

//注册搜索窗口
function showSearchWin(){
	var searchHtml = "<div class='winbox'>公司名：<input type='text' name='searchTxt' id='searchTxt' /><input type='button' name='搜索' value='搜索' onclick='searchMap()'/></div>";
	var searchWin = $("").wBox({title: "地图搜索",opacity:0,html:searchHtml});
	searchWin.showBox();
}

function showCommWin(commTitle,commContent){
	var commHtml= "<div class='winbox'>"+commContent+"</div>";
	var commWin = $("").wBox({title: commTitle,opacity:0,html:commHtml,timeout:1000});
	commWin.showBox();
}

function searchMap(){
	var name = $("#searchTxt").val();
	$.ajax({
      	url: 'company!loadAllInfosByName.j',
      	data: "name="+name,
      	type: 'post',
		success: function(msg){
			var o =  jQuery.parseJSON(msg.result);
			//查找不到信息
			if(o == null || o.length == 0){
				commContent = "查询的信息不存在！";
				commTitle = "出错啦！！";
				showCommWin(commTitle,commContent);
				return;
			}
			//查到信息
			else{
				searchReHtml = "";
				for(var i=0;i<o.length;i++){
					var name = o[i].name;
					var lng = o[i].longitude;
	   				var lat = o[i].latitude;
	   				var level = o[i].level;
	   				searchReHtml += "<div onclick='setMarker("+lng+","+lat+","+level+");'>"+name+":"+lng+":"+lat+":"+level+"</div>";
				}
				var searchReWin =$("").wBox({title: "搜索结果",opacity:0,html:searchReHtml});
				searchReWin.showBox();
			}
		}
	});
}

//添加指定经纬度的标记点
function setMarker(lng,lat,level){
	deleteOverlays();
	
	marker = new GMarker(new GLatLng(lat,lng));
	//在地图上添加标注点
	map.addOverlay(marker);
	
	var point =marker.getPoint();
	map.setCenter(point);
	markerArray.push(marker);//入数组
}

//删除所有Marker
function deleteOverlays(){
	
	if ( markerArray != null  &&  markerArray.length > 0 ){
		for(var m in markerArray){
			map.removeOverlay(markerArray[m]);
		}
        markerArray = [];
//        manager = null;
//        initGMarkerManager(map);
	}
}


